<?php
namespace BoxApi\Controller;

use Think\Controller;

class WechatController extends \HomeController {
	public function _initialize() {
		parent::_initialize();
		$this->storeInfo = BoxModel("addon_wx_config")->load($this->store_id);
		$param = array(
			'token' => $this->storeInfo['token'],
			'appID' => $this->storeInfo['appID'],
			'appsecret' => $this->storeInfo['appsecret'],
		);
		$this->wechatObj = new \Common\Org\weiXin($param);
		/*****************申请成为开发者认证***************/
		if ($_GET["echostr"]) {
			if ($this->wechatObj->checkSignature()) {
				$echoStr = $_GET["echostr"];
				exit($echoStr);
			}
		}
		/******************接受信息操作******************************/
		$this->wechatObj->getMsg();
		$Content = trim($this->wechatObj->postObj->Content);
		$Event = trim($this->wechatObj->postObj->Event);
		$openid = trim($this->wechatObj->postObj->FromUserName);
		$keyName = trim($this->wechatObj->postObj->EventKey);
		$MsgType = trim($this->wechatObj->postObj->MsgType);
		$MediaId = trim($this->wechatObj->postObj->MediaId);
		$postObj = $this->wechatObj->postObj;
		/***用户消息信息***/
		/****扫描二维码*****/
		if ($Event == 'SCAN') {
			$act = $this->scan($keyName);
			exit();
		}
		/***********第一次关注************/
		if ($Event == 'subscribe') {
			$this->firstConcern($openid);
		}

		/***取消关注*****/
		if ($Event == 'unsubscribe') {
			$this->unsubscribe($openid);
		}

		/**保存地理位置*****/
		if ($Event == "LOCATION") {
			$Latitude = "" . $this->wechatObj->postObj->Latitude;
			$Longitude = "" . $this->wechatObj->postObj->Longitude;
			exit();
		}
		/**************事件信息推送*************/
		if ($keyName) {
			$this->makeNews($keyName);
			exit();
		}

	}

	/*****扫二维码***/
	function scan($keyName) {
		$this->makeNews($keyName);
	}

	/***************方法不存在*****/
	function __call($firstvar, $secondvar) {
		header("Content-type:text/html;charset=utf-8");
		echo "你调用的方法{$firstvar}不存在,参数";
		exit();
	}

	/*******first concern首次关注触动方法*******************/
	private function firstConcern($openid) {
		/************推送信息******/
		$this->makeNews("firstConcern");
		/*****通过渠道关注进来的*****/
		$postObj = $this->wechatObj->postObj;
		$keyName = trim($this->wechatObj->postObj->EventKey, "qrscene_");
		if ($keyName) {
			$act = $this->scan($keyName);
			if ($act) {
				$this->$act($keyName, $openid);
			}

		}
		/****保存用户信息*****/
		$data = $this->wechatObj->UserInfo($openid);
		$user_id = Logic("BoxSns", "Member")->WxCreateMember($data);
		exit();
	}

	/*******推送图文********/
	private function makeNews($keyName) {
		$store_id = $this->storeInfo['store_id'];
		$data = $r = S($keyName);
		if (!is_array($r)) {
			$r = $data = BoxModel("addon_wx_feeds")->where("keyName='$keyName' and store_id='$store_id'")->limit(10)->order("sort_order")->field("img as picurl,title,tip as description,url")->select();
			S($keyName, $r, 3600);
		}

		foreach ($data as $k => $one) {
			$data[$k]['picurl'] = ImgDomain . $one['picurl'];
		}
		$openid = trim($this->wechatObj->postObj->FromUserName);
		$data1 = BoxModel("addon_wx_feeds_type")->where("keyName='$keyName' and store_id='$store_id'")->find();
		if ($r) {
			$this->wechatObj->sendNews($openid, $data);
		} else if ($data1['text']) {
			$this->wechatObj->makeText($data1['text']);
		}
	}

	/********取消订阅***********/
	private function unsubscribe($openid) {
		$r = BoxModel("system_member_store")->where("openid='$openid'")->save(array('subscribe' => 2));
	}

}
